﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using DTO;
namespace QLDT
{
    public partial class frm_QLPhanCongChiTiet_SV : DevExpress.XtraEditors.XtraForm
    {
        private int id_tuan;
        private int id_pcdt;
      
        public bool TTThemSua = true;
        public DTO_TaiKhoan taikhoan;
        public frm_QLPhanCongChiTiet_SV()
        {
            InitializeComponent();
        }
        public frm_QLPhanCongChiTiet_SV(DTO_TaiKhoan tk)
        {
            taikhoan = tk;
            InitializeComponent();
        }
        public void Binding()
        {
                txtID_PCDT.DataBindings.Clear();
                txtID_PCDT.DataBindings.Add("Text", grid_DADaLam.DataSource, "ID_PCDT");

                txtTenGiangVien.DataBindings.Clear();
                txtTenGiangVien.DataBindings.Add("Text", grid_DADaLam.DataSource, "TenGV");

                txt_TenDT.DataBindings.Clear();
                txt_TenDT.DataBindings.Add("Text", grid_DADaLam.DataSource, "TenDT");

                txtTenCN.DataBindings.Clear();
                txtTenCN.DataBindings.Add("Text", grid_DADaLam.DataSource, "TenCN");

                txtTenLoaiDT.DataBindings.Clear();
                txtTenLoaiDT.DataBindings.Add("Text", grid_DADaLam.DataSource, "TenloaiDT");

                txtMaKH.DataBindings.Clear();
                txtMaKH.DataBindings.Add("Text", grid_DADaLam.DataSource, "MaKH");

                txtNamBD.DataBindings.Clear();
                txtNamBD.DataBindings.Add("Text", grid_DADaLam.DataSource, "NamBD");

                txtNamKT.DataBindings.Clear();
                txtNamKT.DataBindings.Add("Text", grid_DADaLam.DataSource, "NamKT");
        }
        public void radio()
        {
            if (radioLoctheo.SelectedIndex == 0)
            {
                grid_DADaLam.DataSource = BUS_PhanCongDT.PCDT_Select_MaSV_DangLam(taikhoan.TenTK);
            }
            else
            {
                grid_DADaLam.DataSource = BUS_PhanCongDT.PCDT_Select_MaSV_DaLam(taikhoan.TenTK);
            }
        }
        public void  load()
        {
            grid_QTTH.DataSource = BUS_QTTH.Select_QTTH();

            radio();
            lb_id.DataBindings.Clear();
            lb_id.DataBindings.Add("Text", grid_QTTH.DataSource, "ID_tuan");

            dateyeucau.DataBindings.Clear();
            dateyeucau.DataBindings.Add("Text", grid_QTTH.DataSource, "NgayYeuCau");

     
            dateBaocao.DataBindings.Clear();
            dateBaocao.DataBindings.Add("Text", grid_QTTH.DataSource, "NgayBaoCao");

            memo_baocao.DataBindings.Clear();
            memo_baocao.DataBindings.Add("Text", grid_QTTH.DataSource, "ND_Baocao");

            memo_yeucau.DataBindings.Clear();
            memo_yeucau.DataBindings.Add("Text", grid_QTTH.DataSource, "ND_Yeucau");

           
        }

        private void frm_QLPhanCongChiTiet_SV_Load(object sender, EventArgs e)
        {
            load();
            Binding();
            dateyeucau.Enabled = false;
            dateBaocao.Enabled = false;
            memo_yeucau.Enabled = false;

        }

        private void txtID_PCDT_EditValueChanged(object sender, EventArgs e)
        {
            if (gv_QTTH.RowCount == 0)
            {
                int sotuan = int.Parse( gv_DADaLam.GetRowCellValue(gv_DADaLam.FocusedRowHandle,"Sotuan").ToString());
                if (txtID_PCDT.Text == "")
                    return;
                int id1 = int.Parse( txtID_PCDT.Text);
                for (int i = 1; i <= sotuan; i++)
                {
                    BUS_QTTH.insertQTTH(id1, i);
                }
            }
            if (txtID_PCDT.Text == "")
                return;
            txtTenSV.Text = taikhoan.TenNguoiDung;
            int id = int.Parse(txtID_PCDT.Text);
            grid_QTTH.DataSource = BUS_QTTH.Select_QTTH_ID(id);
            lb_id.DataBindings.Clear();

            lb_id.DataBindings.Add("Text", grid_QTTH.DataSource, "ID_tuan");
            dateyeucau.DataBindings.Clear();

            dateyeucau.DataBindings.Add("Text", grid_QTTH.DataSource, "NgayYeuCau");
            dateBaocao.DataBindings.Clear();
            dateBaocao.DataBindings.Add("Text", grid_QTTH.DataSource, "NgayBaoCao");

            memo_baocao.DataBindings.Clear();
            memo_baocao.DataBindings.Add("Text", grid_QTTH.DataSource, "ND_Baocao");

            memo_yeucau.DataBindings.Clear();
            memo_yeucau.DataBindings.Add("Text", grid_QTTH.DataSource, "ND_Yeucau");
        }

        private void radioLoctheo_SelectedIndexChanged(object sender, EventArgs e)
        {
            load();
            Binding();
        }
        
        private void bt_update_Click(object sender, EventArgs e)
        {

            if (!KTDulieu())
            {
                XtraMessageBox.Show("Nhập đầy đủ nga", "Thông báo");
                return;
            }
            DTO_QTTH QTTH = new DTO_QTTH();
         
            QTTH.ND_Baocao =memo_baocao.Text;
            QTTH.ND_Yeucau = memo_yeucau.Text;
            QTTH.NgayBaoCao = DateTime.Parse(dateBaocao.Text);
            QTTH.NgayYeuCau = DateTime.Parse(dateyeucau.Text);
            int id2 = int.Parse( txtID_PCDT.Text);
            int sotuan2 = int.Parse(lb_id.Text);
       
                if (BUS_QTTH.UpdateQTTH(QTTH, sotuan2, id2))
                    load();
                else
                    XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
        }

        private bool KTDulieu()
        {
        
            if (memo_baocao.Text == "")
                return false;
            if (memo_yeucau.Text == "")
                return false;
            if (dateyeucau.Text == "")
                return false;
            if (dateBaocao.Text == "")
                return false;

            return true;
            
        }

       
       
       
    }
}